package com.yanqu.road.server.cmd.activity.dafuweng;

import com.yanqu.road.entity.activity.dafuweng.DFWConfig;
import com.yanqu.road.entity.activity.dafuweng.DFWUnionData;
import com.yanqu.road.entity.activity.dafuweng.DFWUnionRecord;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.logic.pb.DaFuWengPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manager.cross.battle.DaFuWengDataMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.List;

@Cmd(code = Protocol.C_CROSS_DA_FU_WENG_HANDLE_ZAO_YAO_RETURN, desc = "处理造谣返回")
public class CrossHandleZaoYaoReturnCmd extends CrossUnionCommand {

    int activityId;
    long doServerId;
    long doUserId;
    int type;
    int result;
    MonopolyProto.ZaoYaoSceneMsg sceneMsg = null;
    String unionUid;
    private int recordId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        MonopolyProto.ZaoYaoReturnCrossReqMsg reqMsg = MonopolyProto.ZaoYaoReturnCrossReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        doServerId = reqMsg.getDoServerId();
        doUserId = reqMsg.getDoUserId();
        type = reqMsg.getType();
        result = reqMsg.getResult();
        if (result == 0) {
            sceneMsg = reqMsg.getSceneData();
        }
        unionUid = reqMsg.getUnionUid();
        recordId = reqMsg.getRecordId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        MonopolyProto.CrossHandleZaoYaoRespMsg.Builder builder = MonopolyProto.CrossHandleZaoYaoRespMsg.newBuilder();
        builder.setActivityId(activityId);
        builder.setDoUserId(doUserId);
        builder.setResult(result);
        builder.setType(type);
        if (result == 0) {
            int myRank = -1;
            long userId = sceneMsg.getOwnerData().getUserId();
            CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(activityId,  eGamePlayerEventType.DaFuWengScoreRank.getValue());
            if (rankListModel != null) {
                myRank = rankListModel.getMyRank(userId, serverId);
            }
            MonopolyProto.ZaoYaoSceneMsg.Builder msg = sceneMsg.toBuilder().setRank(myRank);
            if (type == 3) {
                UserBaseInfo userBaseInfo = CrossUserMgr.getUserBaseInfo(doUserId);
                if (userBaseInfo != null) {
                    String infoUnionUid = userBaseInfo.getUnionUid();
                    List<DFWUnionRecord> recordList = DaFuWengDataMgr.getUnionRecord(activityId, infoUnionUid, 1, groupId);
                    DFWUnionRecord record = null;
                    for (DFWUnionRecord tmpRecord : recordList) {
                        if (tmpRecord.getRecordId() == recordId) {
                            record = tmpRecord;
                            break;
                        }
                    }
                    if (record != null) {
                        record.setBeatBackTimes(record.getBeatBackTimes() + 1);
                    }
                }
            }
            MonopolyProto.MonopolyPlayerMsg monopolyPlayerMsg = DaFuWengPb.parseMonopolyPlayerMsg(doUserId, PlayerBasePb.parsePlayerBaseTempMsg(CrossUserMgr.getUserBaseInfo(doUserId)).build());
            DaFuWengDataMgr.addUnionRecord(unionUid, activityId, monopolyPlayerMsg, 1, groupId);
            DFWUnionData unionData = DaFuWengDataMgr.getUnionData(activityId, unionUid);
            DFWConfig dfwConfig = DaFuWengDataMgr.getDfwConfig(activityId);
            DaFuWengDataMgr.changeAddition(dfwConfig.getZaoYaoAddition(), unionData);
            msg.setUnionAddition(DaFuWengDataMgr.getUnionSkillAddition(dfwConfig, unionUid).get(0));
            builder.setSceneData(msg);
        }
        builder.setUnionUid(unionUid);
        YanQuMessage yanQuMessage = YanQuMessageUtils.buildMessage(GameProtocol.S_DA_FU_WENG_ZAO_YAO_RESULT_FROM_CROSS, builder);
        MessageHelper.sendPacket(doServerId, doUserId, yanQuMessage);
    }

    @Override
    public void sendData() {

    }
}
